<?php

/**
 * This is the model class for table "usuarios".
 *
 * The followings are the available columns in table 'usuarios':
 * @property integer $idusuario
 * @property string $nombre
 * @property string $apellido
 * @property string $mail
 * @property string $password
 * @property integer $sexo
 * @property string $fechaNacimiento
 * @property integer $rol
 * @property integer $habilitado
 */
class Usuarios extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Usuarios the static model class
	 */
	 
	 public $verifyCode;
	 
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuarios';
	}

	
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	
	
	
	
	public function getCumpleanos(){
		if(Yii::app()->session['idusuario']!=null){
			$count=Yii::app()->db->createCommand('SELECT count(*) FROM amigos  JOIN usuarios ON amigos.idamigo = usuarios.idusuario WHERE
		 amigos.idusuario='.Yii::app()->session['idusuario'].' AND DATE_FORMAT(usuarios.fechaNacimiento,\'%m-%d\') = DATE_FORMAT(NOW(),\'%m-%d\')')->queryScalar();
			return $count;
		}
		return 0;
	}

	
	
	
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre, apellido, mail, password, fechaNacimiento', 'required'),
			array('sexo, rol, habilitado', 'numerical', 'integerOnly'=>true),
			array('nombre, apellido, mail, password', 'length', 'max'=>30),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idusuario, nombre, apellido, mail, password, sexo, fechaNacimiento, rol, habilitado', 'safe', 'on'=>'search'),
			
			array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idusuario' => 'Idusuario',
			'nombre' => 'Nombre',
			'apellido' => 'Apellido',
			'mail' => 'Mail',
			'password' => 'Password',
			'sexo' => 'Sexo',
			'fechaNacimiento' => 'Fecha Nacimiento',
			'rol' => 'Rol',
			'habilitado' => 'Habilitado',
			'verifyCode'=>'Codigo de Verificacion',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idusuario',$this->idusuario);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('apellido',$this->apellido,true);
		$criteria->compare('mail',$this->mail,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('sexo',$this->sexo);
		$criteria->compare('fechaNacimiento',$this->fechaNacimiento,true);
		$criteria->compare('rol',$this->rol);
		$criteria->compare('habilitado',$this->habilitado);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}